<template>
	<div class="role">
		<div class="container">
			<STform :ref-obj.sync="searchInfo.ref" :data="searchInfo.data"  :field-list="searchInfo.searchFormList" 
					:listTypeInfo="dictList" :inline="true" :rules="searchInfo.rules" label-width=" ">
				<template slot="form-btnSearch">
					<el-button type="primary" @click="doSearch">查询</el-button>
				</template>
				<template slot="form-btnOperation">
					<el-button type="primary" @click="doStatus(1)">通过</el-button>
					<el-button type="danger" @click="doStatus(2)">拒绝</el-button>
				</template>
			</STform>
			<STtable size="mini" :data.sync="tableData" :field-list="tables" :checkBox="true" @doRefresh="fetchData"
					 :pagination="pagination" :loading="loading" @handleEvent="handleEvent">
				<!-- 自定义插槽状态按钮 -->
				<template slot="col-reftype" slot-scope="scope">{{(dicts.gcReplyReftype[scope.row.reftype])? dicts.gcReplyReftype[scope.row.reftype]:"-"}}</template>
				<template slot="col-status" slot-scope="scope">{{(dicts.checkStatus[scope.row.status])? dicts.checkStatus[scope.row.status]:"-"}}</template>
				<template slot="col-user" slot-scope="scope">{{(scope.row.user)? scope.row.user.nick+"["+scope.row.user.id+"]":"-"}}</template>
				<template slot="col-touser" slot-scope="scope">{{(scope.row.touser)? scope.row.touser.nick+"["+scope.row.touser.id+"]":"-"}}</template>
			</STtable>
		</div>
	</div>
</template>

<script>
	import STtable from '@/components/common/STtable'
	import STform from '@/components/common/STform'
	import replyAPI from '@/api/app/reply.js'
	import {showToast} from '@/libs/core.js'
	import consts from './consts.js'
	import {mapState} from 'vuex' //本地取信息
	export default {
		components: {STtable,STform},
		data() {
			return {
				searchInfo: {
					ref: null,
					data: {
					},
					rules: {},
					searchFormList: consts.searchFormList // 查询列表表单列 数据
				},
				multipleSelection: [], //选中数据
				sysPageForm: { //搜索实际请求的字段
					name: '',
				},
				loading: true,
				tableData: [], //列表数据
				tables:consts.tables,
				pagination: { // 分页数据
					pageNum: 1, // 当前页
					pageSize: 20, // 每页条数
					total: 0, //合计条数
					sorts: {id: 'DESC'},//排序信息
					pageSizes: [5, 10, 20, 50, 100], // 分页数量列表
					sizeChange: (val) => {
						this.onSizeChange(val)
					},
					currentChange: (pageNum) => {
						this.onCurrentChange(pageNum)
					},
					sortsChange:(sorts) => {
						this.onSortsChange(sorts);
					},
				}
			}
		},
		async created() {
			await this.fetchData();
			const orm = '\\model\\main\\gc\\Reply';
			await this.$store.dispatch("dictSrv/sysTable", {orm:orm});
			this.loading = false;
		},
		computed: {
			dictList() {
				return this.$store.getters["dictSrv/list"];
			},
			...mapState({
        dicts: (state) => {
          return state.dictSrv.data;
        }
      })
		},
		methods: {
			doSearch() {
				const data = this.searchInfo.data;
				Object.assign(this.sysPageForm, data);
				this.fetchData(this.sysPageForm);
			},
			async doStatus(status) {
				if (this.multipleSelection.length < 1) {
					showToast("请先选中要审核的记录.");
					return;
				}
				const osjon = await replyAPI.doStatus({status:status,
					 table: this.searchInfo.data.table, ids: this.multipleSelection.join(',')});
				if (osjon) {
					const data = this.searchInfo.data;
					this.fetchData(data);
				}
			},
			onSizeChange(pageSize) {
				this.pagination.pageSize = pageSize;
				this.fetchData(this.sysPageForm);
			},
			onCurrentChange(pageNum) {
				this.pagination.pageNum = pageNum;
				this.fetchData(this.sysPageForm);
			},
			onSortsChange(sorts) {
				this.pagination.sorts = sorts;
				this.fetchData(this.sysPageForm);
			},
			async fetchData(params) {
				this.loading = false;
				const { pageNum, pageSize, sorts } = this.pagination;
				const ojson = await replyAPI.getList({start:(pageNum - 1)*pageSize, sorts:sorts, limit:pageSize, ...params});
				if (ojson) {
					this.tableData = ojson.dataset;
					this.pagination.total = ojson.total;
				}
				this.loading = false;
			},
			handleEvent(type, data) {
				switch (type) {
					case 'tableCheck':
						this.multipleSelection = []
						for (const index in data) {
							this.multipleSelection.push(data[index].id);
						}
						break;
				}
			}
		}
	}
</script>

<style scoped>
	.el-tree{
		vertical-align: top;
		height: 560px;
		position: relative;
		overflow:scroll;
		margin-bottom: 20px;
	}
</style>
